#! /usr/bin/env node
import { readFileSync } from "fs";
import { join } from "path";
import { compress } from "./main";

const command = process.argv;

switch (command[2]) {
    case 'help':
    case 'h':
        showHelpStr();
        break;
    case 'version':
    case 'v':
        let verUrl = join(__dirname, '../package.json');
        let verJson = JSON.parse(readFileSync(verUrl).toString())
        console.log('version:', verJson.version);
        break;
    case 'test':
        // testUnzip()
        break;
    case '-e':
    case '-ext':
        let types: string[] = [];
        if (command[3]) {
            let str = command[3] + '';
            if (str.indexOf(',') < 0) {
                if (str.indexOf(' ') >= 0) {
                    types = str.split(' ');
                }
            } else {
                types = str.split(',');
            }
            if (types.indexOf('json') < 0) {
                types.push('json')
            }
            if (command[4]) {
                // 如果有设置具体目录
                compress(command[4], types)
            } else {
                // 没有设置目录，就是命令行运行的根目录
                let path = process.cwd();
                compress(path, types)
            }
        }
        break;
    case '-fs':
        let files: string[] = [];

        if (command[3]) {
            console.log(command[3]);
            let str = command[3] + ''
            files = str.split(',');
            console.log('files:', files);

            compress(files)
        }
        break;
    default:
        let path = command[2]
        if (!path) {
            path = process.cwd();

            // console.log('path:', path);
            compress(path)
        }
        break;
}

function showHelpStr() {
    console.log("==> thank you for using jsonzip <==");
    console.log("if you have question, you can mail me. mail: bfstiger@gmail.com");
    console.log("usage:");

    console.log(`into you want compress dir, input: jsonzip`);
}

